package com.lengxf.pay.web.controller;

import com.lengxf.pay.common.bean.PayOrder;
import com.lengxf.pay.common.bean.QueryOrder;
import com.lengxf.pay.common.bean.RefundOrder;
import com.lengxf.pay.common.bean.RefundOrderQuery;
import com.lengxf.pay.web.common.R;
import com.lengxf.pay.web.service.BizPayService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * demo 可根据具体业务进行调整
 * By Lengxf 2023-11-03 11:47:06
 **/
@RestController
@AllArgsConstructor
@RequestMapping("api/v1")
public class BizPayController {


    private final BizPayService bizPayService;

    static String orderNo = "20231111145902";

    static String refundOrderNo = "20231111145901002";

    @PostMapping("{tenantryId}/toPay")
    public String toPay(@PathVariable("tenantryId") String tenantryId) {
        //demo
        PayOrder payOrder = new PayOrder();
        payOrder.setOrderNo(orderNo);
        payOrder.setPrice(BigDecimal.ONE);
        payOrder.setProductDesc("商品描述");
        payOrder.setPayWay("");
        return bizPayService.toPay(tenantryId, payOrder);
    }

    @PostMapping("refund")
    public R<Void> refund() {
        //demo 此处tenantryId可以根据订单号对应关系查询得到
        RefundOrder refundOrder = new RefundOrder();
        refundOrder.setOrderNo(orderNo);
        refundOrder.setAmount(BigDecimal.ONE);
        refundOrder.setRefundOrderNo(refundOrderNo);
        return R.convert(bizPayService.refund(refundOrder));
    }

    @PostMapping("syncOrder")
    public R<Void> syncOrder() {
        //demo 此处tenantryId可以根据订单号对应关系查询得到
        QueryOrder queryOrder = new QueryOrder();
        queryOrder.setOrderNo(orderNo);
        return R.convert(bizPayService.syncOrder(queryOrder));
    }

    @PostMapping("syncRefundOrder")
    public R<Void> syncRefundOrder() {
        //demo 此处tenantryId可以根据订单号对应关系查询得到
        RefundOrderQuery refundOrderQuery = new RefundOrderQuery();
        refundOrderQuery.setOrderNo(orderNo);
        refundOrderQuery.setRefundOrderNo(refundOrderNo);
        return R.convert(bizPayService.syncRefundOrder(refundOrderQuery));
    }


}
